package com.example.haojiutao.controller.user;

import com.example.haojiutao.common.Result;
import com.example.haojiutao.model.domain.Comment;
import com.example.haojiutao.model.domain.Order;
import com.example.haojiutao.model.domain.User;
import com.example.haojiutao.model.vo.response.CommentGoodsDetailVO;
import com.example.haojiutao.model.vo.response.CommentOrderDetailVO;
import com.example.haojiutao.model.vo.response.OrderGoodsDetailVO;
import com.example.haojiutao.service.CommentService;
import com.example.haojiutao.service.OrderService;
import com.example.haojiutao.service.UserService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
/**
 * @Description: 添加删除评论
 * @Author: zhou_yuan
 * @CreateTime： 2024/1/20 15:47
 */
@Slf4j
@RestController
public class CommentController {
    @Autowired
    private CommentService commentService;

    @Autowired
    private UserService userService;

    @Autowired
    private OrderService orderService;
    /*
     * @Description: 评论商品
     * @Author: zhou_yuan
     * @CreateTime： 2024/1/20 15:49
     * @param: comment
     * @return: com.example.haojiutao.common.Result
     **/
    @PostMapping("/comment")
    public Result insertComment(@RequestBody Comment comment){
        log.info("订单号：{}",comment.getOrderNumber());
        OrderGoodsDetailVO order = orderService.selectByOrderNumber(comment.getOrderNumber());
        log.info("订单:{}",order);
        Comment comment1 = new Comment();
        comment1.setGoodsName(order.getGoodsName());
        comment1.setGoodsId(order.getGoodsId());
        comment1.setSellerName(order.getSellerName());
        comment1.setUserName(order.getUserName());
        comment1.setCommentTime(new Date());
        comment1.setOrderNumber(comment.getOrderNumber());
        comment1.setContent(comment.getContent());
        comment1.setLevel(comment.getLevel());
        comment1.setCommentImg(comment.getCommentImg());
        comment1.setStatus(1);
        System.out.println("-------------------获取的评论为"+comment);
        log.info("评论:{}",comment1);
        Integer rows= commentService.insert(comment1).getCode();
        if(rows!=0){

            return Result.success("评论成功",comment1);

        }
        return Result.faile("提交失败");

    }
    /*
     * @Description: 删除商品
     * @Author: zhou_yuan
     * @CreateTime： 2024/1/20 15:52
     * @param: id
     * @return: com.example.haojiutao.common.Result
     **/
//    @DeleteMapping("/comment")
//    public Result delete(int id){
//
//        return commentService.deleteById(id);
//    }
    @DeleteMapping("/comment/{id}")
    public Result delete1(@PathVariable("id") Integer id){

        return commentService.deleteById(id);
    }
    /*
     * @Description: 查看所有评论
     * @Author: zhou_yuan
     * @CreateTime： 2024/1/20 16:02
     * @param: comment
     * @return: com.example.haojiutao.common.Result
     **/
    @GetMapping("/comment/list/{goodsId}")
    public Result selectbylist(Integer goodsId){
        Comment comment = new Comment();
        comment.setGoodsId(goodsId);
        log.info("列表查询参数 ：{}" ,comment);
        List<Comment> comments =commentService.listByComment(comment);
        PageInfo<Comment> pageInfo=new PageInfo<>(comments);
        return Result.success("查询成功",comments);
    }

    /*
     * @Description: //根据用户ID查询,查询该博客下的所有评论
     * @Author: zhou_yuan
     * @CreateTime： 2024/1/20 16:11
     * @param: userId
     * @return: com.example.haojiutao.common.Result
     **/
    @GetMapping("/comment/{userName}")
    public Result selectByUserId(@PathVariable("userName") String userName){
        log.info("列表查询参数 ：{}");
        List<Comment> comments =commentService.selectByUserIdComments(userName);
        //PageInfo<Comment> pageInfo=new PageInfo<>(comments);
        for(Integer i = 0; i < comments.size(); i++) {
            Comment comment=new Comment();
            comment.setUserName(userName);
            comment.setContent(comment.getContent());
            comment.setGoodsId(comment.getGoodsId());
            comment.setLevel(comment.getLevel());
            comment.setCommentTime(new Date());
// 如果是回复其他用户，设置回复对象的id和用户名
            String touserName =comments.get(i).getToUserName();
            if(touserName != null){
                comment.setToCommentId(comment.getToCommentId());
                comment.setToUserName(userName);
                User reployToUser=userService.getUserInfoByName(userName);
                comment.setToUserName(reployToUser.getUserName());

            }

            User user=userService.getUserInfoByName(comments.get(i).getUserName());
            String UserName= comment.getUserName();
            if(UserName != null){
                comment.setUserName(user.getUserName());
                comments.add(comment);
            }

        }
        return Result.success("comments",comments);


    }

    /*
     * @Description: 根据id查询
     * @Author: zhou_yuan
     * @CreateTime： 2024/1/20 16:20
     * @param: commentId
     * @return: com.example.haojiutao.common.Result
     */
    @GetMapping("/user/comment/{commentId}")
    public Result selectByCommentId(@PathVariable Integer commentId){
        log.info("查看单个商品");
        CommentGoodsDetailVO comment = commentService.selectByCommentId(commentId);
        return Result.success("查询成功！",comment);
    }

    /*
     * @Description: 待评价
     * @Author: zhou_yuan
     * @CreateTime： 2024/1/20 16:26
     * @param: userName
     * @return: com.example.haojiutao.common.Result
     */
    @GetMapping("/user/comment/beforecomment/{userName}")
    public Result selectBeforeComment(@PathVariable String userName) {
        OrderGoodsDetailVO order = new OrderGoodsDetailVO();
        order.setUserName(userName);
        List<OrderGoodsDetailVO> comment1 = commentService.selectBeforeComment(order);
        PageInfo<OrderGoodsDetailVO> pageInfo = new PageInfo<>(comment1);
        System.out.println("!!!!!!!" + order);
        System.out.println("!!!!!!!" + comment1);
        return Result.success("查询成功", pageInfo);
    }

    /*
     * @Description: 可追评
     * @Author: zhou_yuan
     * @CreateTime： 2024/1/20 16:28
     * @param: userName
     * @return: com.example.haojiutao.common.Result
     */
    @GetMapping("/user/comment/recomment/{userName}")
    public Result selectReComment(@PathVariable String userName) {
        CommentGoodsDetailVO comment = new CommentGoodsDetailVO();
        comment.setUserName(userName);
        List<CommentGoodsDetailVO> comment1 = commentService.selectReComment(comment);
        PageInfo<CommentGoodsDetailVO> pageInfo = new PageInfo<>(comment1);
        System.out.println("!!!!!!!" + comment);
        System.out.println("!!!!!!!" + comment1);
        return Result.success("查询成功", pageInfo);
    }




}

